<?php


namespace app\project\controller;


use app\project\model\BannerModel;
use app\project\model\NavModel;
use cmf\controller\AdminBaseController;
use think\facade\Filesystem;
use think\Request;

class NavController extends AdminBaseController
{
        //导航栏后台显示
        public function index(Request $request)
        {
            $res = NavModel::order('id','desc')
                ->paginate(2);
            $this->assign('list',$res);
            return $this->fetch();
        }
        //导航栏后台添加显示
        public function add()
        {
            return $this->fetch();
        }
        //导航栏后台添加
        public function addPost(Request $request)
        {
            $data = $request->param();
            if(empty($data['nav_image']))
            {
                $this->error('图片不能为空');
            }
            if(empty($data['nav_name']))
            {
                $this->error('图片名称不能为空');
            }
            $data['nav_image'] = cmf_get_image_url($data['nav_image']);
            $data['create_time'] = date('Y:m:d H:i:s');
            $res = NavModel::create($data);
            if($res)
            {
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        //导航栏删除
        public function delete(Request $request)
        {
            $id = $request->param('id');
            $res = NavModel::where('id','=',$id)
                ->delete();
            if($res)
            {
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //导航栏修改
        public function edit(Request $request)
        {
            $id = $request->param('id');
            $res = NavModel::where('id','=',$id)
                ->find();
            $this->assign('res',$res);
            return $this->fetch();
        }
        public function editPost(Request $request)
        {
            $data = $request->param();
            if(empty($data['nav_image']))
            {
                $this->error('图片不能为空');
            }
            if(empty($data['nav_name']))
            {
                $this->error('图片名称不能为空');
            }
            $res = NavModel::where('id','=',$data['id'])
                ->update($data);
            if($res)
            {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = NavModel::where('id','=',$id)
            ->find();
        if($res['switch'] == 0)
        {
            NavModel::where('id','=',$id)
                ->update(['switch'=>1]);
            $this->success('修改成功');
        }else{
            NavModel::where('id','=',$id)
                ->update(['switch'=>0]);
            $this->success('修改成功');
        }
    }
}